package converter;

import java.io.File;
import java.io.IOException;

import jp.ne.so_net.ga2.no_ji.jcom.IDispatch;
import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager;

public class JcomTest {

	//JCom调用MS Office转换word为PDF源代码

	public static void word2PDF(String inputFile,String pdfFile){
	        ReleaseManager rm = null;
	        IDispatch app = null;
	        try {
	                rm = new ReleaseManager();
	                app = new IDispatch(rm, "Word.Application");//启动word
	                app.put("Visible", false);    //设置word不可见
	                IDispatch docs = (IDispatch) app.get("Documents");    //获得word中所有打开的文档
	                IDispatch doc = (IDispatch) docs.method("Open", new Object[]{inputFile,false,true});//打开文档
	                doc.method("SaveAs", new Object[]{pdfFile,17});//转换文档为pdf格式
	                doc.method("Close", new Object[]{false});
	                app.method("Quit", null);
	        } catch (Exception e) {
	                 e.printStackTrace();
	        } finally {
	                try {
	                        app=null;
	                        rm.release();
	                        rm = null;
	                } catch (Exception e) {
	                     e.printStackTrace();
	                }
	        }
	    }
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			word2PDF(new File("docs/2016.doc").getCanonicalPath(),new File("docs/2016.pdf").getCanonicalPath());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
